﻿using log4net;
using Mathmall.Administration.common;
using Mathmall.Administration.Mapper;
using Mathmall.Administration.Models;
using Mathmall.Administration.Vo;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
using System.Collections.Generic;
using System.Linq;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;

namespace Mathmall.Administration.Service.impl
{
    public class BrandService : IBrandService
    {
        private readonly BrandMapper _brandMapper;
        private readonly ICategoryBrandRelationService _categoryBrandRelationService;
        private static readonly ILog _logger = LogManager.GetLogger(typeof(UserService));

        public BrandService(BrandMapper brandMapper, ICategoryBrandRelationService categoryBrandRelationService)
        {
            _brandMapper = brandMapper;
            _categoryBrandRelationService = categoryBrandRelationService;
        }

        /// <summary>
        /// 保存品牌信息
        /// </summary>
        /// <param name="brandVo"></param>
        /// <returns></returns>
        public async Task<int> AddBrand(BrandVo brandVo)
        {
            Brand brand = JsonSerializer.Deserialize<Brand>(JsonSerializer.Serialize(brandVo))!;
            return await _brandMapper.AddBrand(brand);
        }

        /// <summary>
        /// 根据BrandId查询品牌信息
        /// </summary>
        /// <param name="brandId"></param>
        /// <returns></returns>
        public async Task<BrandVo?> QuerySingleByBrandId(int brandId)
        {
            Brand? brand = await _brandMapper.QuerySingleByBrandId(brandId);
            if (brand is not null)
            {
                BrandVo brandVo = JsonSerializer.Deserialize<BrandVo>(JsonSerializer.Serialize(brand))!;
                return brandVo;
            }

            return null;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <param name="condition">模糊查询条件</param>
        /// <returns></returns>
        public async Task<PaginatedList<BrandVo>?> PaginatedQuery(int pageNumber, int pageSize, string condition)
        {
            // 查询全部符合条件的数据
            IQueryable<Brand> brands = _brandMapper.QueryListByCondition(condition);

            // 分页处理
            PaginatedList<Brand> brandsPaginatedList =
                await PaginatedList<Brand>.CreateAsync(brands, pageNumber, pageSize);
            List<BrandVo> brandVos = new List<BrandVo>();
            // 通过序列化，将查到的数据放入VO中
            foreach (var item in brandsPaginatedList.Items)
            {
                BrandVo brandVo = JsonSerializer.Deserialize<BrandVo>(JsonSerializer.Serialize(item))!;
                brandVos.Add(brandVo);
            }

            return new PaginatedList<BrandVo>(brandVos, brandsPaginatedList.Count, pageNumber, pageSize);
        }

        /// <summary>
        /// 更新品牌信息
        /// </summary>
        /// <param name="brandVo"></param>
        public async Task<int> UpdateBrand(BrandVo brandVo)
        {
            Brand brand = JsonSerializer.Deserialize<Brand>(JsonSerializer.Serialize(brandVo))!;
            // 更新分类与品牌关系表中品牌的名称
            _categoryBrandRelationService.UpdateBrandName(brandVo.BrandID, brandVo.Name);
            // 更新品牌表中品牌的名称
            return await _brandMapper.Update(brand);
        }

        /// <summary>
        /// 删除品牌
        /// </summary>
        /// <param name="brandIds"></param>
        /// <returns></returns>
        public async Task<int> DeleteBrand(List<int> brandIds)
        {
            return await _brandMapper.Delete(brandIds);
        }
    }
}